/**
 * @author Steve Sedlmayr
 * @mail ssedlmayr@gmail.com
 * @link http://code.google.com/p/chitin/
 * @version 0.1
 
	The MIT License

	Copyright (c) 2009 Steve Sedlmayr

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/

package srs.chitin.data 
{
	import flash.net.SharedObject;
	
	import srs.chitin.data.DiskDataDelegate;
	
	/** 
	 * Manages SharedObject behavior for application data. Fields are filtered to 
	 * prevent data that is not of local concern from being written to ensure 
	 * optimal performance.
	 * 
	 * In other words, only non-exceptional fields get written as represented 
	 * <code>_filterExceptions</code>. Depending on the size of your application, 
	 * the number of exceptions is likely to be quite large.
	 */
	public class LocalDataDelegate extends DiskDataDelegate {
		private static var _argumentsArray:Array = new Array();
		
		public static function get argumentsArray() : Array {
			return _argumentsArray;
		}
		
		public static function set argumentsArray(value:Array) : void {
			_argumentsArray = value;
		}
		
		private static var _callback:Function;
		
		public static function get callback() : Function {
			return _callback;
		}
		
		public static function set callback(value:Function) : void {
			_callback = value;
		}
		
		/**
		 * A list of exceptional fields marked for storage or not depending on the value 
		 * of the <code>_filterExceptions</code> flag on <code>DiskDataDelegate._classInstance</code>.
		 */
		[ArrayElementType("String")]
		private static var _fieldExceptions:Array = [];
		
		public static function get fieldExceptions() : Array {
			return _fieldExceptions;
		}
		
		public static function set fieldExceptions(value:Array) : void {
			_fieldExceptions = value;
		}
		
		private static var _filterExceptions:Boolean = true;
		
		public static function get filterExceptions() : Boolean {
			return _filterExceptions;
		}
		
		private static var _scope:*;
		
		public static function get scope() : * {
			return _scope;
		}
		
		public static function set scope(value:*) : void {
			_scope = value;
		}
		
		private static var _sharedObject:SharedObject;
		
		public static function getSharedObject() : SharedObject {
			return _sharedObject;
		}
		
		public static function setSharedObject(value:String) : void {
			_sharedObject = SharedObject.getLocal(value);
		}
		
		public function LocalDataDelegate() : void {}
		
		public static function save(property:String, value:*) : void {
			DiskDataDelegate.setClassInstance(LocalDataDelegate);
			DiskDataDelegate.save(property, value);
		}
		
		public static function writeToDisk() : void {
			/* Write to disk with NetStatusEvent handling via DiskDataDelegate, 
			   as the write will probably take a couple of seconds. */
			DiskDataDelegate.setClassInstance(LocalDataDelegate);
			DiskDataDelegate.writeToDisk();
		}
	}
}